/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef NET_CONN_TRACK_DEF_H
#define NET_CONN_TRACK_DEF_H

#include <linux/types.h>
#include <linux/bpf.h>

#define CT_CONNECTION_LIFETIME_TCP 21600
#define CT_CONNECTION_LIFETIME_NONTCP 60

#define CT_SYN_TIMEOUT 60
#define CT_CLOSE_TIMEOUT 10

#define CT_REPORT_INTERVAL 5
#define CT_REPORT_FLAGS 0xff

enum ct_action {
    ACTION_UNSPEC,
    ACTION_CREATE,
    ACTION_CLOSE,
};

enum ct_dir {
    CT_EGRESS,
    CT_INGRESS,
    CT_SERVICE,
};

enum ct_status {
    CT_NEW,
    CT_ESTABLISHED,
    CT_REPLY,
    CT_RELATED,
    CT_REOPENED,
};

struct ct_tuple {
    __u32 family;
    __u8 protocol;
    union {
        struct {
            __be32 saddr;
            __be32 daddr;
        } ipv4;
        struct {
            struct in6_addr saddr;
            struct in6_addr daddr;
        } ipv6;
    };

    __be16 sport;
    __be16 dport;
};

struct ct_entry {
    __u32 lifetime;

    // clang-format off
    __u8 rx_closing : 1,
         tx_closing : 1,
         seen_non_syn : 1,
         reserved : 5;
    // clang-format on

    __u8 tx_flags_seen;
    __u8 rx_flags_seen;

    /* last_*x_report is a timestamp of the last time a monitor
     * notification was sent for the transmit/receive direction.
     */
    __u32 last_tx_report;
    __u32 last_rx_report;
};

#endif // NET_CONN_TRACK_DEF_H